<?
require "include/bittorrent.php";
dbconn(false);
loggedinorreturn();

if (get_user_class() < UC_MODERATOR)
	error("Permission denied.");

stdhead("Stats");
begin_main_frame();

$torrents = get_row_count("torrents");
$peers = get_row_count("peers");
$uporder = $_GET["uporder"];
$catorder = $_GET["catorder"];

//Find and show the uploader activity

if ($uporder == "lastul")
	$orderby = "lastupload DESC, username";
elseif ($uporder == "torrents")
	$orderby = "numtorrents DESC, username";
elseif ($uporder == "peers")
	$orderby = "numpeers DESC, username";
else
	$orderby = "username";

$res = query("SELECT users.id, username, MAX(torrents.added) as lastupload, COUNT(DISTINCT torrents.id) as numtorrents, COUNT(peers.id) as numpeers FROM users LEFT JOIN torrents on users.id = torrents.owner LEFT JOIN peers on torrents.id = peers.torrent WHERE users.class >= '" . UC_UPLOADER . "' GROUP BY users.id ORDER BY $orderby") or sqlerr(__FILE__, __LINE__);

begin_frame("Uploader Activity", True);
begin_table();
if (mysql_num_rows($res) == 0)
	print("<b>There aren't any uploaders.</b>\n");
else
{
	print("<tr><td class=colhead><a href=\"" . $_SERVER['PHP_SELF'] . "?uporder=uploader&amp;catorder=$catorder\" class=altlink_white>Uploader</a></td>\n");
	print("<td class=colhead align=center><a href=\"" . $_SERVER['PHP_SELF'] . "?uporder=lastul&amp;catorder=$catorder\" class=altlink_white>Last Upload</a></td>\n");
	print("<td class=colhead align=right><a href=\"" . $_SERVER['PHP_SELF'] . "?uporder=torrents&amp;catorder=$catorder\" class=altlink_white>Torrents</a></td>\n");
	print("<td class=colhead align=right>Percent</td>\n");
	print("<td class=colhead align=right><a href=\"" . $_SERVER['PHP_SELF'] . "?uporder=peers&amp;catorder=$catorder\" class=altlink_white>Peers</a></td>\n");
	print("<td class=colhead align=right>Percent</td></tr>\n");
	while ($uploader = mysql_fetch_array($res))
	{
		print("<tr><td><a href=userdetails.php?id=$uploader[id]><b>$uploader[username]</b></a></td>\n");
		print("<td align=center>" . ($uploader['lastupload']?("$uploader[lastupload] (".get_elapsed_time(strtotime($uploader['lastupload']))." ago)"):"---") . "</td>\n");
		print("<td align=right>$uploader[numtorrents]</td>\n");
		print("<td align=right>" . ($torrents>0?number_format(100 * $uploader['numtorrents']/$torrents,1) . "%":"---") . "</td>\n");
		print("<td align=right>$uploader[numpeers]</td>\n");
		print("<td align=right>" . ($peers>0?number_format(100 * $uploader['numpeers']/$peers,1) . "%":"---") . "</td></tr>\n");
	}
}
end_table();
end_frame();

//Find and show the category activity

if ($catorder == "lastul")
	$orderby = "lastupload DESC, name";
elseif ($catorder == "torrents")
	$orderby = "numtorrents DESC, name";
elseif ($catorder == "peers")
	$orderby = "numpeers DESC, name";
else
	$orderby = "name";

$res = query("SELECT categories.name, MAX(torrents.added) AS lastupload, COUNT(DISTINCT torrents.id) AS numtorrents, COUNT(peers.id) AS numpeers FROM categories LEFT JOIN torrents ON torrents.category = categories.id LEFT JOIN peers ON torrents.id = peers.torrent GROUP BY categories.id ORDER BY $orderby") or sqlerr(__FILE__, __LINE__);

begin_frame("Category Activity", True);
begin_table();
if (mysql_num_rows($res) == 0)
	print("<b>There aren't any categories.</b>\n");
else
{
	print("<tr><td class=colhead><a href=\"" . $_SERVER['PHP_SELF'] . "?uporder=$uporder&amp;catorder=category\" class=altlink_white>Category</a></td>\n");
	print("<td class=colhead align=center><a href=\"" . $_SERVER['PHP_SELF'] . "?uporder=$uporder&amp;catorder=lastul\" class=altlink_white>Last Upload</a></td>\n");
	print("<td class=colhead align=right><a href=\"" . $_SERVER['PHP_SELF'] . "?uporder=$uporder&amp;catorder=torrents\" class=altlink_white>Torrents</a></td>\n");
	print("<td class=colhead align=right>Percent</td>\n");
	print("<td class=colhead align=right><a href=\"" . $_SERVER['PHP_SELF'] . "?uporder=$uporder&amp;catorder=peers\" class=altlink_white>Peers</a></td>\n");
	print("<td class=colhead align=right>Percent</td></tr>\n");
	while ($category = mysql_fetch_array($res))
	{
		print("<tr><td><b>$category[name]</b></a></td>\n");
		print("<td align=center>" . ($category['lastupload']?("$category[lastupload] (".get_elapsed_time(strtotime($category['lastupload']))." ago)"):"---") . "</td>\n");
		print("<td align=right>$category[numtorrents]</td>\n");
		print("<td align=right>" . ($torrents>0?number_format(100 * $category['numtorrents']/$torrents,1) . "%":"---") . "</td>\n");
		print("<td align=right>$category[numpeers]</td>\n");
		print("<td align=right>" . ($peers>0?number_format(100 * $category['numpeers']/$peers,1) . "%":"---") . "</td></tr>\n");
	}
}
end_table();
end_frame();

end_main_frame();
stdfoot();
?>